import subprocess
import re


from app.models.lol_client import LolClientEntity


# 获取进程端口号和token
def get_client_process():
    cmd = 'WMIC PROCESS WHERE name="LeagueClientUx.exe" GET commandline'
    token_pattern = re.compile(r'--remoting-auth-token=([^\s]+)')
    app_port_pattern = re.compile(r'--app-port=([^\s]+)')
    league_client = LolClientEntity()

    try:
        result = subprocess.run(cmd, shell=True, capture_output=True, text=True, encoding='gb2312')
        if result.returncode == 0:
            lines = result.stdout.splitlines()
            for line in lines:
                token_match = token_pattern.search(line)
                port_match = app_port_pattern.search(line)
                if token_match:
                    league_client.token = token_match.group(1).strip('"')
                if port_match:
                    league_client.port = port_match.group(1).strip('"')

        print(f"===Port: {league_client.port}===")
        print(f"===Token: {league_client.token}===")

        return league_client
    except Exception as e:
        print(f"发生错误: {e}")
        return league_client


if __name__ == "__main__":
    league_client_entity1 = get_client_process()
    league_client_entity2 = get_client_process()
    print(league_client_entity2 == league_client_entity1)

    # print(league_client_entity)
    # print(f"=== Port: {repr(league_client_entity.port)} ===")
    # print(f"=== Token: {repr(league_client_entity.token)} ===")

